﻿package net.wangit.jdbc.sqltype;

import java.sql.Clob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

@SuppressWarnings("serial")
public class ClobSqlType extends NullableSqlType {

	public ClobSqlType() {
	}

	public String get(ResultSet rs, String name) throws SQLException {
		Clob clob = rs.getClob(name);
		if (rs.wasNull())
			return null;
		if (clob == null || clob.length() == 0L)
			return "";
		else
			return clob.getSubString(1L, (int) clob.length());
	}

	protected void setNotNull(PreparedStatement st, String value, int index)
			throws SQLException {
		ClobImpl clob = new ClobImpl(value);
		st.setCharacterStream(index, clob.getCharacterStream(), (int) clob
				.length());
	}

	public int sqlType() {
		return Types.CLOB;
	}
}
